package com.fhz.mvvmshopjava.ui.classify;

import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.fhz.mvvmshopjava.R;
import com.fhz.mvvmshopjava.adapter.ClassifyLeftAdapter;
import com.fhz.mvvmshopjava.adapter.ClassifyRightAdapter;
import com.fhz.mvvmshopjava.core.BaseFragment;
import com.fhz.mvvmshopjava.databinding.ClassifyFragmentBinding;
import com.fhz.mvvmshopjava.entity.Category;
import com.fhz.mvvmshopjava.ui.sell.SellActivity;
import com.fhz.utilsfhz.ApiResponse;

import java.util.ArrayList;
import java.util.List;

import dagger.hilt.android.AndroidEntryPoint;

@AndroidEntryPoint
public class ClassifyFragment extends BaseFragment<ClassifyViewModel, ClassifyFragmentBinding> implements ClassifyLeftAdapter.MyClick, ClassifyRightAdapter.MyRightClick {

    List<Category> rightList;
    ClassifyRightAdapter classifyRightAdapter;
    @Override
    protected int getLayoutId() {
        return R.layout.classify_fragment;
    }

    @Override
    public void click(int i) {
        mViewModel.getCategory(String.valueOf(++i)).observe(this,listApiResponse -> {
            rightList.clear();
            rightList.addAll(listApiResponse.getData());
            classifyRightAdapter.notifyDataSetChanged();
        });

    }

    @Override
    public void rightClick(int i) {
        SellActivity.launch(getActivity(),String.valueOf(i));
    }

    @Override
    protected void initData() {
        List<Category> list = new ArrayList<>();
        ClassifyLeftAdapter classifyLeftAdapter = new ClassifyLeftAdapter(list);
        classifyLeftAdapter.setMyClick(this);

        mBinding.classifyLeftRv.setAdapter(classifyLeftAdapter);
        //左边的列表适配器
        mBinding.classifyLeftRv.setLayoutManager(new LinearLayoutManager(getActivity()));
        mViewModel.getCategory("0").observe(this, listApiResponse -> {
            list.clear();
            list.addAll(listApiResponse.getData());
            classifyLeftAdapter.notifyDataSetChanged();
        });



        //右边的列表适配器
        mBinding.classifyRightRv.setLayoutManager(new LinearLayoutManager(getActivity()));
        rightList = new ArrayList<>();
        classifyRightAdapter = new ClassifyRightAdapter(rightList);
        classifyRightAdapter.setMyRightClick(this);
        mBinding.classifyRightRv.setAdapter(classifyRightAdapter);
        mViewModel.getCategory("1").observe(this, listApiResponse -> {
            rightList.clear();
            rightList.addAll(listApiResponse.getData());
            classifyLeftAdapter.notifyDataSetChanged();
        });

    }

    @Override
    protected ClassifyViewModel createViewModel() {
        return new ViewModelProvider(this).get(ClassifyViewModel.class);
    }
}